#!/usr/bin/env python3
"""
检查数据库中的股票数据
"""
from database.connection import db_manager

def check_stock_data():
    """检查数据库中的股票数据"""
    try:
        with db_manager.get_connection() as conn:
            cursor = conn.cursor(dictionary=True)
            
            # 检查股票基本信息
            print("🔍 检查股票基本信息...")
            cursor.execute("SELECT COUNT(*) as count FROM stock_basic_info")
            basic_count = cursor.fetchone()['count']
            print(f"✅ stock_basic_info 表中有 {basic_count} 条记录")
            
            if basic_count > 0:
                cursor.execute("SELECT * FROM stock_basic_info LIMIT 5")
                basic_info = cursor.fetchall()
                print("📋 前5条股票基本信息:")
                for stock in basic_info:
                    print(f"  - {stock['symbol']}: {stock['name']}")
            
            # 检查恐贪指数数据
            print("\n🔍 检查恐贪指数数据...")
            cursor.execute("SELECT COUNT(*) as count FROM stock_fear_greed")
            fear_greed_count = cursor.fetchone()['count']
            print(f"✅ stock_fear_greed 表中有 {fear_greed_count} 条记录")
            
            if fear_greed_count > 0:
                cursor.execute("SELECT * FROM stock_fear_greed ORDER BY timestamp DESC LIMIT 5")
                fear_greed_data = cursor.fetchall()
                print("📋 最新的5条恐贪指数数据:")
                for data in fear_greed_data:
                    print(f"  - {data['symbol']}: 指数={data['fear_greed_index']}, 情绪={data['sentiment']}, 时间={data['timestamp']}")
            
            # 检查K线数据
            print("\n🔍 检查K线数据...")
            cursor.execute("SELECT COUNT(*) as count FROM stock_kline")
            kline_count = cursor.fetchone()['count']
            print(f"✅ stock_kline 表中有 {kline_count} 条记录")
            
            if kline_count > 0:
                cursor.execute("SELECT * FROM stock_kline ORDER BY timestamp DESC LIMIT 5")
                kline_data = cursor.fetchall()
                print("📋 最新的5条K线数据:")
                for data in kline_data:
                    print(f"  - {data['symbol']}: 收盘价={data['close_price']}, 时间={data['timestamp']}")
            
            # 检查特定股票的数据
            print("\n🔍 检查AAPL.US的数据...")
            cursor.execute("SELECT * FROM stock_basic_info WHERE symbol = 'AAPL.US'")
            aapl_basic = cursor.fetchone()
            if aapl_basic:
                print(f"✅ 找到AAPL.US基本信息: {aapl_basic['name']}")
                
                cursor.execute("SELECT * FROM stock_fear_greed WHERE symbol = 'AAPL.US' ORDER BY timestamp DESC LIMIT 1")
                aapl_fear_greed = cursor.fetchone()
                if aapl_fear_greed:
                    print(f"✅ 找到AAPL.US恐贪指数: {aapl_fear_greed['fear_greed_index']}")
                else:
                    print("❌ 没有找到AAPL.US的恐贪指数数据")
                
                cursor.execute("SELECT * FROM stock_kline WHERE symbol = 'AAPL.US' ORDER BY timestamp DESC LIMIT 1")
                aapl_kline = cursor.fetchone()
                if aapl_kline:
                    print(f"✅ 找到AAPL.US价格数据: {aapl_kline['close_price']}")
                else:
                    print("❌ 没有找到AAPL.US的价格数据")
            else:
                print("❌ 没有找到AAPL.US的基本信息")
            
    except Exception as e:
        print(f"❌ 检查数据时出错: {e}")

if __name__ == "__main__":
    check_stock_data()
